<template>
<view>
<!-- 外卖模式的点单 --> 

<view class="waimaiContainer">
  <scroll-view scroll-y class="leftScrollType">
     <view v-for="(type, index) in productType" :key="index" :data-type="type" data-level="one" @tap="bindTypeItem" :class="(type.active?'activeTypeItem':'TypeItem')" :style="'border-bottom:' + (type.active?'2rpx solid '+setting.platformSetting.defaultColor:'') + ';color:' + (type.active?setting.platformSetting.defaultColor:'')">
        <block v-if="!!type.iconImage">
          <image class="TypeItem_iamge" mode="aspectFit" :src="type.iconImage"></image>
        </block>
        <block v-else></block>
        <text>{{type.name}}</text>
        </view>
      
      <!-- <view class='ToOrderView_container'>
        <block wx:if='{{pushItem.length > 0}}'>
          <view class='ToOrderView ToOrderView1' data-link='order_list_0.html?easyStatusName=全部订单&easyStatus=0' bindtap='toOrderPage'>
            <image mode='aspectFill' src='https://image1.sansancloud.com/xianhua/2019_12/16/15/54/40_186.jpg?x-oss-process=style/preview_120'></image>
            <text>订单</text>
          </view>
        </block>
        <block wx:else>
        <view class='ToOrderView ToOrderView2' data-link='order_list_0.html?easyStatusName=全部订单&easyStatus=0' bindtap='toOrderPage'>
          <image mode='aspectFill' src='https://image1.sansancloud.com/xianhua/2019_12/16/15/54/40_186.jpg?x-oss-process=style/preview_120'></image>
          <text>订单</text>
        </view>
       </block>
      </view> -->
       
      <view class="bottomHeight"></view>
  </scroll-view>

  <!-- 右边的商品列表 -->
  <scroll-view scroll-y @scrolltolower="onReachBottom2" class="RightProductContainer">
    <scroll-view scroll-x class="two_pro_type" v-if="productTypeTwo&&productTypeTwo.length>1">
      <text class="two_pro_type_item" v-for="(result, index) in productTypeTwo" :key="index" :style="'background:' + (result.active?setting.platformSetting.secondColor:'') + ';color:' + (result.active?setting.platformSetting.defaultColor:'')" @click.stop="bindTypeItem" :data-type="result" data-level="two">{{result.name}}</text>
    </scroll-view>
    <image mode="widthFix" :src="currentItem.adverImg" style="width:100%;" v-if="currentItem.adverImg&&currentItem.adverImg!=', '"></image>
    <view v-if="!productData" class="nonePro">
      <image mode="widthFix" src="https://image1.sansancloud.com/xianhua/2019_12/16/15/54/55_950.jpg?x-oss-process=style/preview_120"></image>
      <text>没有相关商品</text>
    </view>
    <view class="productItem" v-for="(result, index) in productData" :key="index" :data-info="result" :data-link="'user_product_detail.html?id=' + result.id" @tap="linkUrl">
        <view class="productItemImage">
          <image mode="aspectFill" class="productImg" :src="result.imagePath + '?x-oss-process=style/preview'"></image>
          <image class="promotion_icon" v-if="result.promotion>0" src="https://image1.sansancloud.com/jianzhan/2019_1/12/18/6/10_230.jpg"></image>
        </view>
        <view class="productInfo">
          <view class="productInfo_name">{{result.name}}</view>
          <view class="productInfo_decribe">
            <!-- <text class='small_decribe'>月售100件|好评99.9%</text> -->
            <text class="productInfo_promotionName" :style="'background:' + setting.platformSetting.defaultColor" v-if="result.saleStrategyName">{{result.saleStrategyName}}</text>
          </view>
          <view class="productInfo_Price">

            <view class="price_group">
              库存：
              <!-- <text class='price' wx:if="{{result.promotion>0}}">￥{{result.promotionPrice}}</text> -->
              <text class="price">{{result.stock}}</text>
              <!-- <text class='price' wx:else>￥{{result.price}}</text>
              <text class='price' wx:if='{{result.maxPrice>result.price}}'>-{{result.maxPrice}}</text> -->
              <text v-if="result.unitShow" style="color:#bbb;font-size:20rpx;margin-left:4rpx;letter-spacing:4rpx;">{{result.unitShow}}</text>
              <!-- <text wx:if='{{result.maxPrice<=result.price&&result.tagPrice>result.price}}' class="tag_price">￥{{result.tagPrice}}</text> -->
            </view>
            <view class="productInfo_Nums">
              <block v-if="!result.measureItem">
                <block v-if="result.inCarCount>0">
                  <image src="https://image1.sansancloud.com/xianhua/2019_12/16/15/55/32_567.jpg?x-oss-process=style/preview_120" :data-id="index" @click.stop="subNum"></image>
                  <text class="font1">{{result.inCarCount}}</text>
                </block>
                <image src="https://image1.sansancloud.com/xianhua/2019_12/16/15/55/32_666.jpg?x-oss-process=style/preview_120" :data-id="index" @click.stop="addNum"></image>
              </block>
              <block v-else>
                <block v-if="result.inCarCount>0">
                  <view class="PRICE" :style="'background:' + setting.platformSetting.defaultColor"> +{{result.inCarCount}}</view>
                </block>
                <view class="choose_measureItem" :data-id="index" @click.stop="chooseMeasureItem">选规格</view>
                </block>
            </view>

          </view>
        </view>
    </view>
    <view class="bottomHeight"></view>
  </scroll-view>


</view>

  <block v-if="pushItem.length > 0">
  <view class="bottomCartContainer" :style="'background:' + setting.platformSetting.defaultColor">
    <view class="cartInfo" @tap="showZheshao">
       <view class="imageCartContainer flexCenter">
         <image src="https://image1.sansancloud.com/xianhua/2019_12/16/15/56/3_253.jpg?x-oss-process=style/preview_120"></image>
         <view class="countCartNum">{{countGood}}</view>
       </view>
       <view class="Price">￥<text class="PRICE">{{countPrice}}</text></view>
    </view>
    <!-- <view class="sureButn" @tap="linkUrl" data-link="shopping_car_list_new.html">一键代发</view> -->
    <view class="sureButn"  @tap="createOrder">一键代发</view>
  </view>
  </block>

<view v-if="pushItem.length > 0" class="waimai_cart_container_forTap"> 
    <view class="Tap_cart">
      <block v-if="!showType"><view class="Tap_cart_v1" :style="'background:' + setting.platformSetting.defaultColor" @tap="showZheshao">^</view></block>
      <block v-else><view class="Tap_cart_v2" @tap="showZheshao">v</view> </block>
     </view>
  <scroll-view scroll-y class="waimai_cart_container" :animation="animationData">
    <view class="waimai_cart_item" v-for="(cartI, index) in pushItem" :key="index">
        <view class="waimai_cart_item_info">
          <view class="waimai_cart_item_info_name">{{cartI.item.name}}</view>
          <block v-if="!!cartI.measureCartensian">
          <view class="waimai_cart_item_info_measureCartensian">
            <view v-for="(mesure, index2) in cartI.measureCartensian.measurements" :key="index2">{{mesure.measureName}}</view>
          </view>
          </block>
          <view class="waimai_cart_item_info_price">
            ￥<text class="waimai_cart_item_info_price2">{{cartI.carItemPrice}}</text>
          </view>
        </view>
        <view class="waimai_cart_item_nums">
                <image src="https://image1.sansancloud.com/xianhua/2019_12/16/15/55/32_567.jpg?x-oss-process=style/preview_120" :data-id="index" @click.stop="subCartNum"></image>
                <text class="font1">{{cartI.count}}</text>
            
              <image src="https://image1.sansancloud.com/xianhua/2019_12/16/15/55/32_666.jpg?x-oss-process=style/preview_120" :data-id="index" @click.stop="addCartNum"></image>
        </view>
    </view>
  </scroll-view>
</view>

<view class="zhezhao" v-if="showType" @tap="closeZhezhao"></view>


<!-- 规格 -->
  <view v-if="showGuigeType" class="measureItemsContainer">
    <view class="measureItemsCard">
      <view class="cardHead">
        <view class="cardHeadTip">
          <view :style="'background:' + setting.platformSetting.defaultColor"></view>
          <view :style="'background:' + setting.platformSetting.defaultColor"></view>
          <view :style="'background:' + setting.platformSetting.defaultColor"></view>
        </view>
        <view class="cardHeadName">{{MeasureItem.name}}</view>
        <view class="closeCard" @tap="closeGuigeZhezhao">
         <icon type="clear" :color="setting.platformSetting.defaultColor" size="23"></icon>
        </view>
      </view>
      <view class="cardMid" v-for="(types, index) in MeasureItem.measureTypes" :key="index">
        <view class="cardMidName">{{types.name}}</view>
        <radio-group class="cardMidGuigeContainer" :name="types.name" @change="chooseMeasure">
          <label v-for="(Measure, i) in types.productAssignMeasure" :key="i" :data-index="index + '-' + i" @tap="radioChange">
            <view :class="(types.checkedMeasureItem == i? 'active':'')" :style="'background:' + (types.checkedMeasureItem == i? setting.platformSetting.defaultColor:'') + ';border: 1px solid ' + (types.checkedMeasureItem == i? setting.platformSetting.defaultColor:'')">{{Measure.measureName}}</view>
            <radio :checked="types.checkedMeasureItem == i" :value="Measure.measureTypeName + '-' + Measure.id" hidden="true"></radio>
          </label>
        </radio-group>
      </view>
     
      <view class="submitMeasure" :style="'background:' + setting.platformSetting.defaultColor">
        <view class="submitMeasure_price">￥ <text class="PRICE">{{measurementJson.price}}</text></view>
        <view class="submitMeasure_addto" @tap="submitMeasure">加入购物车</view>
      </view>

    </view>
  </view>

<view class="zhezhao" v-if="showGuigeType"></view>
</view>
</template>

<script>

export default {
  data() {
    return {
      animationData: {},
      setting: null,
      // setting   
      productData: [],
      // 商品数据 
      productType: null,
      productTypeTwo: null,
      sysWidth: 320,
      //图片大小
      tab: '',

      /* 显示或影藏 */
      showType: false,
      show0: false,
      show1: false,
      show2: false,
      topName: {
        SearchProductName: "" //头部搜索的

      },
      focusTypeItem: null,
      bindProductTypeIndex: null,
      ProductshowWay: 1,
      // ProductshowWay列表显示方法 
      typeSearch: '',
      // typeSearch的字体 
      canRefresh: true,

      /* 购物车 */
      cart: [],
      pushItem: [],
      countGood: 0,
      countPrice: 0,
      //规格
      showGuigeType: false,
      checkedMeasureItem: 0,
      MeasureItem: null,
      measurementJson: {
        price: 0
      } //规格价格
      ,
      listPage: {
        page: 1,
        pageSize: 0,
        totalSize: 0,
        curpage: 1
      },

      /* 全部参数 */
      params: {
        categoryId: 0,
        page: 1
      },
      currentItem: "",
      loginUser: ""
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("===options=====", options);
    let that = this;

    if (getApp().globalData.loginUser) {
      for (let i in options) {
        for (let j in that.params) {
          if (i.toLowerCase() == j.toLowerCase()) {
            that.params[j] = options[i];
          }
        }
      }

      console.log(that.params);
      that.checkState();
    } else {
      getApp().globalData.addLoginListener(this);
      getApp().globalData.showToastLoading('loading', true);
      console.log("====setTimeout1=====");
      that.setTimeoutLogin(that.conut);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getCart();
    this.getPriceAndCount();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.listPage.page = 1;
    this.params.page = 1;
    this.getData(this.params, 2);
    wx.showNavigationBarLoading();
    wx.hideNavigationBarLoading(); //完成停止加载

    wx.stopPullDownRefresh(); //停止下拉刷新
  },
  methods: {
      /* 创建订单 */
      createOrder: function () {
        if (!getApp().globalData.checkShopOpenTime()) {
          return;
        }
      
        var listPro = {
          shopId: '',
          selectedIds: '',
          tableId:'',
        };
        console.log('--------下单了----------',this.cart[0].carItems);
      
        if (this.cart.length == 0) {
          return;
        }
      
        let pushItem = this.cart[0].carItems;
      
        if (pushItem.length == 0) {
          return;
        }
      
        for (let i = 0; i < pushItem.length; i++) {
          listPro.shopId = pushItem[i].belongShop;
          listPro.selectedIds += pushItem[i].id + ',';
        }
      
        let that = this;
        let customIndex = getApp().globalData.AddClientUrl(" /list_promotions_by_car_items.html", listPro, 'post');
        wx.request({
          url: customIndex.url,
          data: customIndex.params,
          header: getApp().globalData.headerPost,
          method: 'POST',
          success: function (res) {
            console.log('------这里应该有promotionId数组--------');
            console.log(res);
            wx.hideLoading();
            if(that.tableId){
              listPro.tableId = that.tableId;
            }
            if (!!res.data.promotionId) {
              listPro.promotionId = res.data.promotionId;
            } else {
              listPro.promotionId = '0';
            }
      
            console.log(res.data);
            console.log(listPro);
            that.createOrder22(listPro);
          },
          fail: function (res) {
            wx.hideLoading();
            getApp().globalData.loadFail();
          }
        });
      },
      
      /* 创建订单 */
      createOrder22: function (params) {
        var customIndex = getApp().globalData.AddClientUrl("/shopping_car_list_item_create_order.html", params, 'post');
        var that = this; 
        getApp().globalData.showToastLoading('loading', true);
        wx.request({
          url: customIndex.url,
          data: customIndex.params,
          header: getApp().globalData.headerPost,
          method: 'POST',
          success: function (res) {
            console.log(res);
      
            if (!!res.data.orderNo) {
              wx.navigateTo({
                url: '/pages/edit_order/index?orderNo=' + res.data.orderNo+"&orderType=7"
              });
            } else {
              wx.showToast({
                title: res.data.errMsg,
                image: '/wxcomponents/images/icons/tip.png',
                duration: 2000
              });
            }
          },
          fail: function (res) {
            wx.hideLoading();
            getApp().globalData.loadFail();
          }
        });
      },
    //获取产品分类
    getProductType: function (e, typeText) {
      console.log("====e=====", e);
      var that = this;
      let categoryId;
      let type = "init";
      let productType = that.productType;
      let productTypeTwo = [];

      if (e) {
        if (e.currentTarget) {
          categoryId = e.currentTarget.dataset.id;
          type = e.currentTarget.dataset.type;
        } else {
          categoryId = e;
          type = typeText;
        }
      } else {
        categoryId = 0;
      }

      console.log("===type==", type);
      var customIndex = getApp().globalData.AddClientUrl("/wx_get_categories_only_by_parent.html", {
        categoryId: categoryId
      }); // wx.showLoading({
      //   title: 'loading'
      // })

      getApp().globalData.showToastLoading('loading', true);
      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          wx.hideLoading();
          console.log("==res====", res.data);

          if (res.data.errcode == 0) {
            if (type == "init") {
              productType = res.data.relateObj;
              productType.unshift({
                id: 0,
                name: "全部"
              });
              that.setData({
                productType: productType,
                productTypeTwo: []
              });
              console.log("productType", that.productType);
            } else {
              productTypeTwo = res.data.relateObj;
              let all = {
                id: categoryId,
                name: '全部',
                parentId: categoryId,
                colorAtive: "#888",
                active: true
              };
              productTypeTwo.splice(0, 0, all);
              that.setData({
                productTypeTwo: productTypeTwo
              });
              console.log("productTypeTwo", that.productTypeTwo);
            }
          } else {}

          console.log("==productType==", productType);

          if (productType.length != 0) {
            for (let i = 0; i < productType.length; i++) {
              productType[i].colorAtive = '#888';
              productType[i].active = false;
            }

            if (type == "init") {
              productType[0].colorAtive = that.setting.platformSetting.defaultColor;
              productType[0].active = true;
              that.setData({
                currentItem: productType[0]
              });
            } else {
              for (let i = 0; i < productType.length; i++) {
                if (categoryId == productType[i].id) {
                  that.setData({
                    currentItem: productType[i]
                  });
                  productType[i].colorAtive = that.setting.platformSetting.defaultColor;
                  productType[i].active = true;
                }
              }
            }
          }

          that.setData({
            productType: productType
          });
          console.log("that.data.productType", that.productType);
          console.log("that.data.productTypeTwo", that.productTypeTwo);
          wx.hideLoading();
        },
        fail: function (res) {
          console.log("fail");
          wx.hideLoading();
          getApp().globalData.loadFail();
        }
      });
    },
    // getProductType: function (categoryId,type) {
    //   var customIndex = app.AddClientUrl("/wx_get_categories_only_by_parent.html", { categoryId: categoryId || 0 })
    //   // wx.showLoading({
    //   //   title: 'loading'
    //   // })
    //   app.showToastLoading('loading', true)
    //   var that = this
    //   wx.request({
    //     url: customIndex.url,
    //     header: app.header,
    //     success: function (res) {
    //       wx.hideLoading()
    //       console.log("getProductType", res.data)
    //       if (res.data.errcode == 0) {
    //         that.setData({ productType: res.data.relateObj })
    //       } else {
    //         that.setData({ productType: that.data.productType })
    //       }
    //       that.data.productType.unshift({ id: categoryId||0, name: "全部" })
    //       for (let i = 0; i < that.data.productType.length; i++) {
    //         that.data.productType[i].colorAtive = '#888';
    //       }
    //       that.data.productType[0].colorAtive = that.data.setting.platformSetting.defaultColor;
    //       that.data.productType[0].active = true;
    //       that.setData({ productType: that.data.productType })
    //       console.log("that.data.productType",that.data.productType)
    //       wx.hideLoading()
    //     },
    //     fail: function (res) {
    //       console.log("fail")
    //       wx.hideLoading()
    //       app.loadFail()
    //     }
    //   })
    // },
    //跳转到订单页面
    linkUrl: function (e) {
      console.log("===e====", e);
      let linkUrl = e.currentTarget.dataset.link;
      getApp().globalData.linkEvent(linkUrl);
    },

    /* 点击遮罩层 */
    closeZhezhao: function () {
      this.getData(this.params, 2);
      this.setData({
        showType: false
      });
      let animation = wx.createAnimation({
        duration: 400,
        timingFunction: 'ease-out'
      });
      animation.height(0).step();
      this.setData({
        animationData: animation.export()
      });
    },
    showZheshao: function () {
      this.getData(this.params, 2);
      this.setData({
        showType: !this.showType
      });
      let showType2 = this.showType;
      let animation = wx.createAnimation({
        duration: 400,
        timingFunction: 'ease-in-out'
      });

      if (showType2) {
        animation.height(250).step();
      } else {
        animation.height(0).step();
      }

      this.setData({
        animationData: animation.export()
      });
    },

    /* 点击分类大项 */
    bindTypeItem: function (event) {
      console.log(event.currentTarget.dataset.type);
      let id = event.currentTarget.dataset.type.id;
      let level = event.currentTarget.dataset.level;
      let that = this;

      if (level == 'one') {
        for (let i = 0; i < that.productType.length; i++) {
          if (that.productType[i].id == id) {
            that.productType[i].active = true;
            that.setData({
              currentItem: that.productType[i]
            });
          } else {
            that.productType[i].active = false;
          }
        }

        that.getProductType(id, 'first');
        that.setData({
          productType: that.productType
        });
      } else {
        for (let i = 0; i < that.productTypeTwo.length; i++) {
          if (that.productTypeTwo[i].id == id) {
            that.productTypeTwo[i].active = true;
            that.setData({
              currentItem: that.productType[i]
            });
          } else {
            that.productTypeTwo[i].active = false;
          }
        }

        that.setData({
          productTypeTwo: that.productTypeTwo
        });
      }

      that.listPage.page = 1;
      that.params.page = 1;

      if (event.currentTarget.dataset.type.id == "all") {
        that.params.categoryId = '';
        that.getData(this.params, 2);
        that.setData({
          showType: false,
          bindProductTypeIndex: null
        });
        var allItem = {
          id: ""
        };
        that.setData({
          focusTypeItem: allItem
        });
      } else {
        that.setData({
          focusTypeItem: event.currentTarget.dataset.type
        });
        var focus = event.currentTarget.dataset.type; //if (focus.children.length == 0) {

        that.params.categoryId = focus.id;
        that.getData(this.params, 2);
        that.setData({
          showType: false,
          bindProductTypeIndex: null
        }); //}
      }
    },
    ChangeParam: function (params) {
      var returnParam = "";

      for (let i in params) {
        returnParam += "&" + i + "=" + params[i];
      }

      console.log(returnParam);
      return returnParam;
    },

    /* 拿出价格和购物车里面的东西 */
    showPrice: function () {
      var cartDataItem = this.cart[0].carItems;
      var pushItem = [];
      /*  var countGood = 0
       var countPrice = 0 */

      for (let i = 0; i < cartDataItem.length; i++) {
        pushItem.push(cartDataItem[i]);
      }
      /*  for (let i = 0; i < pushItem.length; i++) {
         countGood += parseInt(pushItem[i].count)
         countPrice += parseInt(pushItem[i].count) * pushItem[i].carItemPrice
       } */


      this.setData({
        pushItem: pushItem
        /*     countGood: countGood,
            countPrice: countPrice */

      });
      /* console.log(pushItem)
      console.log(countGood)
      console.log(countPrice) */
    },

    /* 产品的加减 */
    subNum: function (event) {
      if (!getApp().globalData.checkIfLogin()) {
        return;
      }

      let productData = this.productData;
      let index = event.currentTarget.dataset.id;
      let focusProduct = productData[index];
      console.log(focusProduct);
      var params = {};
      params.cartesianId = focusProduct.measureItem;
      params.productId = focusProduct.id;
      params.shopId = focusProduct.belongShopId;

      if (focusProduct.inCarCount == 1) {
        params.count = 0;
        params.type = 'change';
      } else {
        params.count = 1;
        params.type = 'dec';
      }

      --focusProduct.inCarCount;
      this.postParams(params);
    },

    /* 初始化Nums  用传值方式 */
    getPriceAndCount: function () {
      let params = {};
      params.cartesianId = '0', params.productId = '0';
      this.postParams(params);
    },
    addNum: function (event) {
      if (!getApp().globalData.checkIfLogin()) {
        return;
      }

      let productData = this.productData;
      let index = event.currentTarget.dataset.id;
      let focusProduct = productData[index];
      console.log(focusProduct);
      ++focusProduct.inCarCount; // this.setData({ productData: productData})

      var params = {};
      params.cartesianId = focusProduct.measureItem;
      params.productId = focusProduct.id;
      params.shopId = focusProduct.belongShopId;
      params.count = 1;
      params.type = 'add';
      this.postParams(params);
    },

    /* 购物车的加减 */
    subCartNum: function (e) {
      var that = this;
      var pushItem = this.pushItem;
      var focusCartItem = null;
      var index = e.currentTarget.dataset.id;
      console.log(pushItem);
      focusCartItem = pushItem[index];
      let params = {
        cartesianId: '0',
        productId: '',
        shopId: '',
        count: '',
        type: ''
      }; //focusCartItem.count--;

      params.cartesianId = focusCartItem.measureCartesianId;
      params.productId = focusCartItem.productId;
      params.shopId = focusCartItem.belongShop;

      if (focusCartItem.count == 1) {
        params.count = 0;
        params.type = 'change';
      } else {
        params.count = 1;
        params.type = 'dec';
      }

      this.postParams(params);
    },
    addCartNum: function (e) {
      var that = this;
      var pushItem = this.pushItem;
      var focusCartItem = null;
      var index = e.currentTarget.dataset.id;
      focusCartItem = pushItem[index]; //focusCartItem.count++;

      var params = {
        cartesianId: '0',
        productId: '',
        shopId: '',
        count: '',
        type: ''
      };
      params.cartesianId = focusCartItem.measureCartesianId;
      params.productId = focusCartItem.productId;
      params.shopId = focusCartItem.belongShop;
      params.count = 1;
      params.type = 'add';
      this.postParams(params);
    },

    /* 获取数据 */
    getData: function (param, ifAdd) {
      //根据把param变成&a=1&b=2的模式
      console.log("getData", param);

      if (!ifAdd) {
        ifAdd = 1;
      }

      var customIndex = getApp().globalData.AddClientUrl("/more_product_list.html", param); // wx.showLoading({
      //   title: 'loading'
      // })

      getApp().globalData.showToastLoading('loading', true);
      var that = this;
      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          console.log(res.data);
          that.listPage.pageSize = res.data.pageSize;
          that.listPage.curPage = res.data.curPage;
          that.listPage.totalSize = res.data.totalSize;
          let dataArr = that.productData;

          if (ifAdd == 2) {
            dataArr = [];
          }

          if (!res.data.result || res.data.result.length == 0) {
            that.setData({
              productData: null
            });
          } else {
            if (dataArr == null) {
              dataArr = [];
            }

            dataArr = dataArr.concat(res.data.result);
            that.setData({
              productData: dataArr
            });
          }

          wx.hideLoading();
        },
        fail: function (res) {
          console.log("fail");
          wx.hideLoading();
          getApp().globalData.loadFail();
        },
        complete: function () {
          that.setData({
            canRefresh: true
          });
        }
      });
    },
    more_product_list_URLFun: function (params) {
      let resule = getApp().globalData.AddClientUrl("/more_product_list.html", params);
      return resule;
    },
    toProductDetail: function (event) {
      console.log("--------toProductDetail------");
      console.log(event.currentTarget.dataset.info);
      var info = event.currentTarget.dataset.info;
      wx.navigateTo({
        url: '../productDetail/index?id=' + info.id + "&addShopId=" + info.belongShopId
      });
    },

    /* 购物车加减 */
    postParams: function (data) {
      var that = this;
      var customIndex = getApp().globalData.AddClientUrl("/change_shopping_car_item.html", data, 'post');
      wx.request({
        url: customIndex.url,
        data: customIndex.params,
        header: getApp().globalData.headerPost,
        method: 'POST',
        success: function (res) {
          console.log(res.data);
          wx.hideLoading();
          that.setData({
            countGood: res.data.totalCarItemCount,
            countPrice: res.data.totalCarItemPrice,
            productData: that.productData
          });
          that.getCart();
        },
        fail: function (res) {
          wx.hideLoading();
          getApp().globalData.loadFail();
        }
      });
    },

    /* 创建订单 */
    createOrder: function () {
      if (!getApp().globalData.checkShopOpenTime()) {
        return;
      }

      var listPro = {
        shopId: '',
        selectedIds: ''
      };
      console.log('--------下单了----------');

      if (this.cart.length == 0) {
        return;
      }

      let pushItem = this.cart[0].carItems;

      if (pushItem.length == 0) {
        return;
      }

      for (let i = 0; i < pushItem.length; i++) {
        listPro.shopId = pushItem[i].belongShop;
        listPro.selectedIds += pushItem[i].id + ',';
      }

      let that = this;
      let customIndex = getApp().globalData.AddClientUrl(" /list_promotions_by_car_items.html", listPro, 'post');
      wx.request({
        url: customIndex.url,
        data: customIndex.params,
        header: getApp().globalData.headerPost,
        method: 'POST',
        success: function (res) {
          console.log('------这里应该有promotionId数组--------');
          console.log(res);
          wx.hideLoading();

          if (!!res.data.promotionId) {
            listPro.promotionId = res.data.promotionId;
          } else {
            listPro.promotionId = '0';
          }

          console.log(res.data);
          console.log(listPro);
          that.createOrder22(listPro);
        },
        fail: function (res) {
          wx.hideLoading();
          getApp().globalData.loadFail();
        }
      });
    },

    /* 创建订单 */
    createOrder22: function (o) {
      var customIndex = getApp().globalData.AddClientUrl("/shopping_car_list_item_create_order.html", o, 'post');
      var that = this; // wx.showLoading({
      //   title: 'loading',
      //   mask: true
      // })

      getApp().globalData.showToastLoading('loading', true);
      wx.request({
        url: customIndex.url,
        data: customIndex.params,
        header: getApp().globalData.headerPost,
        method: 'POST',
        success: function (res) {
          console.log("=======+++++++=======", res);

          if (!!res.data.orderNo) {
            wx.navigateTo({
              url: '/pages/edit_order/index?orderNo=' + res.data.orderNo
            });
          } else {
            wx.showToast({
              title: res.data.errMsg,
              image: '/wxcomponents/images/icons/tip.png',
              duration: 2000
            });
          }
        },
        fail: function (res) {
          wx.hideLoading();
          getApp().globalData.loadFail();
        }
      });
    },

    /* 获取购物车 */
    getCart: function () {
      var customIndex = getApp().globalData.AddClientUrl("Client.User.CarItemList");
      var that = this; //拿custom_page

      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          console.log(res.data);

          if (res.data.errcode == '10001') {
            that.setData({
              cart: []
            });
          } else {
            if (!!res.data.result.length) {
              that.setData({
                cart: res.data.result
              });
              that.showPrice();
            } else {
              that.setData({
                cart: []
              });
              that.setData({
                pushItem: []
              });
              that.setData({
                showType: false
              });
            }
          }
        },
        fail: function (res) {}
      });
    },
    //提交规格产品
    submitMeasure: function (id) {
      var that = this;
      let focusProduct = this.MeasureItem;
      let measurementJson = this.measurementJson;
      let data = {};
      console.log(focusProduct);
      ++focusProduct.inCarCount;
      data.cartesianId = measurementJson.id;
      data.productId = focusProduct.id;
      data.shopId = focusProduct.belongShopId;
      data.count = 1;
      data.type = 'add';
      var customIndex = getApp().globalData.AddClientUrl("/change_shopping_car_item.html", data, 'post');
      wx.request({
        url: customIndex.url,
        data: customIndex.params,
        header: getApp().globalData.headerPost,
        method: 'POST',
        success: function (res) {
          console.log('--------add----------');
          console.log(res.data);
          that.setData({
            countGood: res.data.totalCarItemCount,
            countPrice: res.data.totalCarItemPrice,
            productData: that.productData,
            showGuigeType: false
          });
          that.showZheshao();
          that.getCart();
          wx.hideLoading();
        },
        fail: function (res) {
          wx.hideLoading();
          getApp().globalData.loadFail();
        }
      });
    },
    //获取规格价格参数
    get_measure_cartesionFun: function () {
      console.log(this.MeasureItem);
      let productId = this.MeasureItem.id;
      let postStr = '';

      for (let i = 0; i < this.MeasureParams.length; i++) {
        postStr += this.MeasureParams[i].value + ',';
      }

      let param = {};
      param.productId = productId;
      param.measureIds = postStr;
      let customIndex = getApp().globalData.AddClientUrl("/get_measure_cartesion.html", param);
      var that = this;
      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          console.log(res.data);
          that.setData({
            measurementJson: res.data
          });
        },
        fail: function (res) {
          console.log("fail");
          getApp().globalData.loadFail();
        },
        complete: function () {}
      });
    },

    /* 初始化 选规格 */
    chooseMeasureItem: function (event) {
      let productData = this.productData;
      let index = event.currentTarget.dataset.id;
      let focusProduct = productData[index];

      for (let i = 0; i < focusProduct.measureTypes.length; i++) {
        focusProduct.measureTypes[i].checkedMeasureItem = 0; //初始化选择的数据

        let param = {};
        param.name = focusProduct.measureTypes[i].name;
        param.value = focusProduct.measureTypes[i].productAssignMeasure[0].id;
        this.MeasureParams.push(param);
      }

      this.setData({
        showGuigeType: true,
        MeasureItem: focusProduct
      });
      this.get_measure_cartesionFun();
    },
    //选择规格小巷的---显示
    radioChange: function (e) {
      let index = e.currentTarget.dataset.index;
      let indexJson = getApp().globalData.getSpaceStr(index, '-'); //console.log(indexJson)

      let focusItem = this.MeasureItem;
      focusItem.measureTypes[indexJson.str1].checkedMeasureItem = indexJson.str2;
      this.setData({
        MeasureItem: focusItem
      });
    },
    //选择规格小巷---获取数据
    chooseMeasure: function (e) {
      console.log(e.detail.value);
      let chooseMeasureJson = getApp().globalData.getSpaceStr(e.detail.value, '-');
      console.log(chooseMeasureJson);

      for (let i = 0; i < this.MeasureParams.length; i++) {
        if (this.MeasureParams[i].name == chooseMeasureJson.str1) {
          this.MeasureParams[i].value = chooseMeasureJson.str2;
        }
      }

      this.get_measure_cartesionFun();
    },
    closeGuigeZhezhao: function () {
      this.setData({
        showGuigeType: false
      });
      this.MeasureParams = [];
    },
    loginSuccess: function (user) {
      console.log("pre apply mendian login success call back!", user);
      this.checkState();
    },
    loginFailed: function (err) {
      console.log("login failed!!");
    },
    checkState: function () {
      let that = this;
      console.log('======checkState.loginUser======', getApp().globalData.loginUser);
      that.setData({
        setting: getApp().globalData.setting,
        loginUser: getApp().globalData.loginUser
      });
      that.getProductType(that.params.categoryId);
      that.getData(that.params, 2);
    },
    setTimeoutLogin: function (conuData) {
      let that = this;
      console.log("====setTimeout-init=====", conuData);
      that.conut = conuData;
      that.conut += 2;

      if (that.conut <= 5) {
        setTimeout(function () {
          if (getApp().globalData.loginUser) {
            wx.hideLoading();
          } else {
            that.setTimeoutLogin(that.conut);
          }
        }, that.conut * 1000);
      } else {
        wx.showModal({
          title: '失败了',
          content: '请求失败了，请下拉刷新！'
        });
      }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom2: function () {
      var that = this;

      if (this.canRefresh) {
        this.setData({
          canRefresh: false
        });
        setTimeout(function () {
          if (that.listPage.totalSize > that.listPage.curPage * that.listPage.pageSize) {
            that.listPage.page++;
            that.params.page++;
            that.getData(that.params);
          }
        }, 300);
      }
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>